/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY. 
 * See the GNU General Public License for more details.
 * 
 * Copyright (C) 2011 CellObject.net
 */
package net.cellobject.javacodeformatter.action;

import net.cellobject.javacodeformatter.log.FormatterLog;
import net.cellobject.javacodeformatter.view.FormatterView;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.IWorkbenchWindowActionDelegate;
import org.eclipse.ui.PartInitException;


public class OpenFormatterViewActionDelegate implements IWorkbenchWindowActionDelegate {
	  private IWorkbenchWindow window;

	   public void init(IWorkbenchWindow window) {
	      this.window = window;
	   }

	   public void selectionChanged(IAction action, ISelection selection) {
	   }

	   public void run(IAction action) {
	      // Get the active page.
	      if (window == null)
	         return;
	      IWorkbenchPage page = window.getActivePage();
	      if (page == null)
	         return;
	   
	      // Open and activate the Formatter view.
	      try {
	         page.showView(FormatterView.ID);
	      }
	      catch (PartInitException e) {
	         FormatterLog.logError("Failed to open the Formatter view", e);
	      }
	   }

	   public void dispose() {
	   }
}
